<!-- 
Author: 摘星fy
Editor: SublimeText
  Date: 2023-11-26 23:54:12
    QQ: 273259755
    WX: y273259755
-->
<!DOCTYPE html>
<html lang="zh-cn">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="shortcut icon" href="./img/favicon.ico" type="image/x-icon">
  <title>脑图 | By 摘星fy</title>
  <style>
    * {padding: 0; margin: 0; font: inherit; box-sizing: border-box;}
    html, body {height: 100%;}

    #app {width: 100%; height: 100%; overflow: hidden;}
    #app canvas {width: 100%; height: 100%; vertical-align: top;}
    #app canvas:focus {background: #f3fbff;}

    #select-dir {position: fixed; right: 15px; top: 15px;}

    .brain-graph-editor {min-width: 200px; min-height: 100px;}
  </style>
</head>
<body>
  
<div id="app">
  <canvas id="canvas"></canvas>
</div>

<div id="select-dir"></div>

<script type="module">
import './lib/app-aux.js'
import BrainGraphControl from './lib/lib-brain-graph-control.js'
import data from './data/addr-zip.js'

console.time(data.length + '个节点 执行耗时')
const brainGraph = window.brainGraph = new BrainGraphControl({
  canvas: document.getElementById('canvas'),
  data,
  direction: 'l',
  conf: {
    rowSpace: 100
  },
})
console.timeEnd(data.length + '个节点 执行耗时')

const d = window.d = brainGraph.d

{
  const el = document.getElementById('select-dir')
  const setSelectByPath = (node) => {
    const path = []
    let t = node

    while (d.mapPid[t.id]?.length) {
      t = d.mapPid[t.id][0]
    }

    while (t) {
      path.push({
        val: t.id,
        name: t.name,
        list: d.mapPid[t.pid]
      })
      t = d.mapId[t.pid]
    }

    path.reverse()

    el.innerHTML = path.map(v => {
      return `<select>${v.list.map(v => `<option value="${v.id}">${v.name}</option>`.trim()).join('')}</select> `
    }).join('')

    Array.from(el.children).forEach((select, idx) => {
      select.value = path[idx].val
      select.onchange = () => {
        setSelectByPath(d.mapId[select.value])
      }
    })

    brainGraph.pathHighlightSimple(d.mapId[path.last()?.val])
    brainGraph.setTranslate(50, 20)
    brainGraph.render()
  }

  setSelectByPath(d.mapId[26011])
}
</script>

</body>
</html>